package com.mflex.mwms.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.mflex.mwms.R;
import com.mflex.mwms.adapter.TransferOnAdapter;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.Task;
import com.mflex.mwms.model.TaskDetail;
import com.mflex.mwms.utils.MMKVUtils;
import com.mflex.mwms.utils.MessageUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TransferOnActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    @BindView(R.id.stv_Warehouse)
    EditText stvWarehouse;

    @BindView(R.id.stv_BoxNumber)
    EditText stvBoxNumber;

    @BindView(R.id.button_clear)
    Button buttonClear;

    @BindView(R.id.button_query)
    Button buttonQuery;

    @BindView(R.id.lvTaskList)
    ListView lvTaskList;

    MiniLoadingDialog mMiniLoadingDialog;

    List<Task> taskList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer_on);
        ButterKnife.bind(this);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        taskList = new ArrayList<>();
        TransferOnAdapter transferOnAdapter = new TransferOnAdapter(this, taskList);
        lvTaskList.setAdapter(transferOnAdapter);

        stvWarehouse.setFocusable(false);
        stvWarehouse.setText(MMKVUtils.getString("Warehose", ""));

        stvBoxNumber.setOnKeyListener(this);
        buttonClear.setOnClickListener(this);
        buttonQuery.setOnClickListener(this);
        lvTaskList.setOnItemClickListener(this);
        requestTask(this.getCurrentFocus());
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_Warehouse:
//                    checkWarehouse(view);
                    break;
                case R.id.stv_BoxNumber:
                    requestTaskScan(view);
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_clear:
                clearButtonMd(view);
                break;
            case R.id.button_query:
                queryButtonMd(view);
                break;
            default:
                break;
        }
    }

    private void clearButtonMd(View v) {
        stvBoxNumber.setText("");
        stvBoxNumber.requestFocus();
        taskList = new ArrayList<>();
        TransferOnAdapter transferOnAdapter = new TransferOnAdapter(this, taskList);
        lvTaskList.setAdapter(transferOnAdapter);
    }

    private void queryButtonMd(View v) {
        requestTask(v);
    }

    private void requestTask(View v) {
        mMiniLoadingDialog.show();
        XHttp.get("api/Task/GetTransferTaskListForWH")
                .params("BoxNumber", stvBoxNumber.getText().toString())
                .params("ToWarehouse", MMKVUtils.getString("Warehose", ""))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<List<Task>>, List<Task>>(new SimpleCallBack<List<Task>>() {

                    @Override
                    public void onSuccess(List<Task> response) {
                        mMiniLoadingDialog.dismiss();
                        taskList = response;
                        TransferOnAdapter transferOnAdapter = new TransferOnAdapter(TransferOnActivity.this, taskList);
                        lvTaskList.setAdapter(transferOnAdapter);

                        stvBoxNumber.setText("");
                        stvBoxNumber.requestFocus();

                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvBoxNumber.setText("");
                        stvBoxNumber.requestFocus();
//                        if (e.getCode() == 100) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_warning)
//                                    .title(R.string.tip_warning)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
//                        if (e.getCode() == 200) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_tip)
//                                    .title(R.string.tip_infos)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
                        MessageUtils.errorMsgAndVoice(TransferOnActivity.this, e);
                    }
                }) {
                });
    }

    private void requestTaskScan(View v) {
        mMiniLoadingDialog.show();
        XHttp.get("api/Task/GetTransferTaskListForWH")
                .params("BoxNumber", stvBoxNumber.getText().toString())
                .params("ToWarehouse", MMKVUtils.getString("Warehose", ""))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<List<Task>>, List<Task>>(new SimpleCallBack<List<Task>>() {

                    @Override
                    public void onSuccess(List<Task> response) {
                        mMiniLoadingDialog.dismiss();
                        taskList = response;
                        TransferOnAdapter transferOnAdapter = new TransferOnAdapter(TransferOnActivity.this, taskList);
                        lvTaskList.setAdapter(transferOnAdapter);

                        for(TaskDetail taskDetail : taskList.get(0).getTaskDetails()){
                            if(StringUtils.equals(stvBoxNumber.getText().toString(),taskDetail.getDetailName())){
                                taskDetail.setChecked(true);
                            }
                        }
                        stvBoxNumber.setText("");
                        stvBoxNumber.requestFocus();

                        Intent intent = new Intent(TransferOnActivity.this, TransferOnDetailActivity.class);
                        intent.putExtra("taskDetail", JsonUtil.toJson(response.get(0).getTaskDetails()));
                        intent.putExtra("taskNo", response.get(0).getTaskNo());
                        startActivityForResult(intent, 0);

                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvBoxNumber.setText("");
                        stvBoxNumber.requestFocus();
//                        if (e.getCode() == 100) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_warning)
//                                    .title(R.string.tip_warning)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
//                        if (e.getCode() == 200) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_tip)
//                                    .title(R.string.tip_infos)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
                        MessageUtils.errorMsgAndVoice(TransferOnActivity.this, e);
                    }
                }) {
                });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(TransferOnActivity.this, TransferOnDetailActivity.class);
        intent.putExtra("taskDetail", JsonUtil.toJson(taskList.get(i).getTaskDetails()));
        intent.putExtra("taskNo", taskList.get(i).getTaskNo());
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        requestTask(TransferOnActivity.this.getCurrentFocus());
    }
}